package org.example.todolist.service.impl;

import org.example.todolist.entity.Task;
import org.example.todolist.mapper.TaskMapper;
import org.example.todolist.service.TaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.time.LocalDateTime;
import java.util.ArrayList;


/**
 * 任务服务实现类
 */
@Service
public class TaskServiceImpl implements TaskService {

    @Autowired
    private TaskMapper taskMapper;

    @Override
    public Task createTask(Task task) {
        // 设置创建时间
        task.setCreatedAt(LocalDateTime.now());
        // 如果状态为空，设置为未完成状态
        if (task.getStatus() == null) {
            task.setStatus(0); // 0表示未完成
        }
        // 插入任务
        int resultCount = taskMapper.insert(task);
        if (resultCount == 0) {
            return null;
        }
        return task;
    }

    @Override
    public Task getTaskById(Integer id) {
        return taskMapper.selectById(id);
    }

    @Override
    public ArrayList<Task> getTasksByListId(Integer listId) {
        return taskMapper.selectByListId(listId);
    }

    @Override
    public boolean updateTask(Task task) {
        int resultCount = taskMapper.update(task);
        return resultCount > 0;
    }

    @Override
    public boolean deleteTask(Integer id) {
        int resultCount = taskMapper.deleteById(id);
        return resultCount > 0;
    }

    @Override
    public boolean updateTaskStatus(Integer id, Integer status) {
        int resultCount = taskMapper.updateStatus(id, status);
        return resultCount > 0;
    }
}
